﻿using AutoMapper;
using B.S.Procurement.Domain;
using B.S.Procurement.Domain.Sale;
using B.S.Procurement.Error;
using B.S.Procurement.Write.Api.Application.Command.DetailsCommand;
using MediatR;
using SqlSugar;

namespace B.S.Procurement.Write.Api.Application.CommandHander.DetailsCommandHander
{
    public class DelDetailsCommandHander : IRequestHandler<DelDetailsCommand, ApiResult<int>>
    {
        private readonly IMapper mapper;
        private readonly ISqlSugarClient db;

        public DelDetailsCommandHander(IMapper mapper, ISqlSugarClient db)
        {
            this.mapper = mapper;
            this.db = db;
        }

        public async Task<ApiResult<int>> Handle(DelDetailsCommand request, CancellationToken cancellationToken)
        {
            ApiResult<int> result = new ApiResult<int>();


            var model = mapper.Map<PurchaseDetails>(request);

            model.Del = false;

            var list = await db.Queryable<Details>().FirstAsync(x => x.DetailsId == request.DetailsId);



            result.Data = await db.Updateable<Details>(list).ExecuteCommandAsync();

            if (result.Data > 0)
            {
                result.Code = ApiEnum.success;
                result.Msg = "删除成功";
            }
            else
            {
                result.Code = ApiEnum.error;
                result.Msg = "删除失败";
            }

            return result;
        }
    }
}
